package com.xhsd.zlong.generatecode.process.freemark.imp;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xhsd.zlong.generatecode.model.IColumn;
import com.xhsd.zlong.generatecode.model.ITable;
import com.xhsd.zlong.generatecode.process.IProcessor;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import freemarker.template.Version;

public class Processor implements IProcessor {

	@Override
	public void process(ITable table, String genDir) throws IOException,
			TemplateException {

		Configuration cfg = new Configuration();

		File templateDir = new File("template");

		cfg.setDirectoryForTemplateLoading(templateDir);

		cfg.setObjectWrapper(new DefaultObjectWrapper());

		cfg.setDefaultEncoding("UTF-8");

		cfg.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER);

		cfg.setIncompatibleImprovements(new Version(2, 3, 20));

		File[] templates = templateDir.listFiles();

		File generatedDir = new File(genDir);

		if (!generatedDir.exists())
			generatedDir.mkdirs();

		Map<String, Object> root = convertITableToMap(table);

		for (File template : templates) {

			if (template.isFile()) {
				String name = template.getName();

				Template temp = cfg.getTemplate(name);

				String file = generatedDir.getAbsolutePath() + File.separator
						+ name;
				
				//处理文件名				
				file = file.replace("javaNameUpper", table.getJavaNameUpper());

				temp.process(root, new FileWriter(file));

				System.out.println("FILE :" + file + " WAS GENERATED!");
			}
		}
	}

	private Map<String, Object> convertITableToMap(ITable table) {

		Map<String, Object> map = new HashMap<String, Object>();

		map.put("name", table.getName());

		map.put("javaName", table.getJavaName());

		map.put("javaNameUpper", table.getJavaNameUpper());

		map.put("javaNameLowerAll", table.getJavaNameLowerAll());

		map.put("comment", table.getComment());

		map.put("pk", convertIColumnToMap(table.getPk()));

		map.put("columns", convertIColumnsToMapList(table.getColumns()));

		return map;
	}

	private List<Map<String, Object>> convertIColumnsToMapList(
			List<IColumn> columns) {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();

		for (IColumn column : columns) {

			result.add(convertIColumnToMap(column));
		}

		return result;
	}

	private Map<String, Object> convertIColumnToMap(IColumn column) {

		Map<String, Object> map = new HashMap<String, Object>();

		map.put("name", column.getName());

		map.put("javaName", column.getJavaName());

		map.put("javaNameUpper", column.getJavaNameUpper());

		map.put("type", column.getType());

		map.put("comment", column.getComment());

		map.put("pri", column.isPri());

		return map;
	}

}
